package com.eqdd.yiqidian.fragment;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.eqdd.annotation.api.DealCodeInjector;
import com.eqdd.annotation.datasave.DealCodeClass;
import com.eqdd.yiqidian.FaxianFragmentCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.BGABannerAdapter;
import com.eqdd.yiqidian.adapter.SectionDividerItemDecoration;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseQuickAdapter;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseViewHolder;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.annotation.DealCode;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.CodeRule;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.EnterBean;
import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.bean.rebuild.QiyeImgBean;
import com.eqdd.yiqidian.bean.rebuild.TipBean;
import com.eqdd.yiqidian.bean.rebuild.TrumpetListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.listener.OnMyClickListener;
import com.eqdd.yiqidian.rxbus2.RxBus;
import com.eqdd.yiqidian.rxbus2.Subscribe;
import com.eqdd.yiqidian.rxbus2.ThreadMode;
import com.eqdd.yiqidian.ui.discover.JifenbangActivity;
import com.eqdd.yiqidian.ui.discover.LejuandanActivity;
import com.eqdd.yiqidian.ui.discover.QiyeBlankActivity;
import com.eqdd.yiqidian.ui.discover.RongyuqingActivity;
import com.eqdd.yiqidian.ui.discover.friendcircle.FriendCircleActivity;
import com.eqdd.yiqidian.ui.discover.gonggao.GonggaoActivity;
import com.eqdd.yiqidian.ui.discover.workcircle.WorkCircleActivity;
import com.eqdd.yiqidian.ui.gongneng.renshi.RenshiActivity$$DealCodeInjector;
import com.eqdd.yiqidian.ui.gongneng.renshi.qiyewenhua.QiyeImgActivity;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.web.WebActivity;
import com.javonlee.dragpointview.OnPointDragListener;
import com.javonlee.dragpointview.view.AbsDragPointView;
import com.javonlee.dragpointview.view.DragPointView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lvzhihao on 17-4-15.
 */

public class FaxianFragment extends BaseFragment {

    private FaxianFragmentCustom dataBinding;
    public List<EnterBean> enterBeans = new ArrayList<>();


    private Class[] classes = new Class[]{
            WorkCircleActivity.class,
//            FriendCircleActivity.class,
            GonggaoActivity.class,
            QiyeBlankActivity.class,
            RongyuqingActivity.class,
            LejuandanActivity.class,
            JifenbangActivity.class
    };
    private int[] resIds = {R.mipmap.banenr1, R.mipmap.banner2,
            R.mipmap.banner3};
    private User user;
    private ArrayList<String> tips;
    private ArrayList<String> imgUrls;
    private ArrayList<TipBean> tipBeen;
    private SlimAdapterEx<TipBean> slimAdapterEx;
    private Object register;

    @DealCodeClass(classes = {GonggaoActivity.class})
    public void updateGonggao(int num) {
        slimAdapterEx.getDataItem(1).setNum(num);
        slimAdapterEx.notifyItemChanged(1);
    }

    @Subscribe(code = CodeRule.STAFF_XIAOLABA, threadMode = ThreadMode.MAIN)
    public void refreshXiaolaba() {
        refreshLamb();
    }

    @Override
    protected void setView() {
        dataBinding.marqueeText.setText("公司立足郑州，面向全国，以饱满的热情努力工作，为您的企业持续发展和快速转型注入我们自己的一份力量");
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        dataBinding.recyclerView.addItemDecoration(new SectionDividerItemDecoration(getActivity(), 3).setLastBootomHeght(0).setHeadHeight(0));
        dataBinding.recyclerView.setNestedScrollingEnabled(false);
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_15, new SlimInjector<TipBean>() {
            @Override
            public void onInject(TipBean data, IViewInjector injector) {
                injector.text(R.id.tv_title, data.getTitle())
                        .image(R.id.iv_head, data.getResId());
                if (data.getNum() > 0) {
                    injector.gone(R.id.drag_point_view);
                    injector.visible(R.id.red_dot);
                    injector.text(R.id.drag_point_view, data.getNum() + "");
                } else {
                    injector.gone(R.id.drag_point_view);
                    injector.gone(R.id.red_dot);

                }

            }
        }).attachTo(dataBinding.recyclerView).updateData(tipBeen);
        ItemClickSupport.addTo(dataBinding.recyclerView).setOnItemClickListener(
                (rv, p, v) -> {
                    TipBean dataItem = slimAdapterEx.getDataItem(p);
                    if (classes[p] != null) {
//                        if (dataItem.getNum() > 0) {
//                            for (int i : dataItem.getCodes()) {
//                                GreenDaoUtil.setNum(i, 0);
//                            }
//                        }
                        startActivity(classes[p]);
                    }
                }
        );
        dataBinding.bannerViewpager.setAdapter(new BGABannerAdapter(getActivity()));
        dataBinding.bannerViewpager.setData(resIds);
        OkGo.<HttpResult<List<QiyeImgBean>>>post(HttpConfig.BASE_URL + HttpConfig.QIYE_IMG_LIST)
                .params("companyId", user.getCompanyId())
                .execute(new JsonCallBack<HttpResult<List<QiyeImgBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<List<QiyeImgBean>>> response) {
                        HttpResult<List<QiyeImgBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            imgUrls = new ArrayList<>();
                            tips = new ArrayList<>();
                            for (int i = 0; i < httpResult.getItems().size(); i++) {
                                imgUrls.add(httpResult.getItems().get(i).getImageUrl());
                                tips.add(httpResult.getItems().get(i).getTitle());
                            }
                            dataBinding.bannerViewpager.setData(imgUrls, tips);
                            dataBinding.bannerViewpager.setDelegate((banner, itemView, model, position) -> {
                                Bundle bundle = new Bundle();
                                bundle.putString(Config.TITLE, httpResult.getItems().get(position).getTitle());
                                bundle.putString(Config.URL, httpResult.getItems().get(position).getUrl());
                                startActivity(WebActivity.class, bundle);
                            });

                        }
                    }
                });
        refreshLamb();
        if (register != null) {
            ((FaxianFragment$$DealCodeInjector) register).update1();
        }
    }

    private void refreshLamb() {
        OkGo.<HttpResult<TrumpetListResultBean>>post(HttpConfig.BASE_URL + HttpConfig.TRUMPET_NEW)
                .params("userGuid", user.getGuid())
                .params("comid", user.getCompanyId())
                .execute(new JsonCallBack<HttpResult<TrumpetListResultBean>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<TrumpetListResultBean>> response) {
                        HttpResult<TrumpetListResultBean> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            dataBinding.marqueeText.setText(httpResult.getItems().getContent());
                        }
                    }
                });
    }


    @Override
    protected void initData() {
        register = DealCodeInjector.register(this, RxBus.class);
        RxBus.getDefault().register(this);
        user = GreenDaoUtil.getUser();
        tipBeen = new ArrayList<>();
        tipBeen.add(new TipBean(R.mipmap.gongzuo, "工作圈"));
//        tipBeen.add(new TipBean(R.mipmap.kehuquan, "朋友圈"));
        tipBeen.add(new TipBean(R.mipmap.gonggao, "通知公告", CodeRule.STAFF_TONGZHI, CodeRule.STAFF_GONGGAO));
//        tipBeen.add(new TipBean(R.mipmap.kanb, "企业看板"));
//        tipBeen.add(new TipBean(R.mipmap.gere, "荣誉墙"));
//        tipBeen.add(new TipBean(R.mipmap.qiye, "乐捐单"));
//        tipBeen.add(new TipBean(R.mipmap.shezh, "积分榜"));
    }


    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_faxian, null, false);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroy() {
        RxBus.getDefault().unregister(this);
        DealCodeInjector.unregister(this, RxBus.class, register);
        super.onDestroy();
    }
}
